package io.openkunlun.javadsl.client;

import io.openkunlun.javadsl.serialization.DaprSerializer;
import io.openkunlun.javadsl.v1.DaprProtos;
import lombok.Builder;
import lombok.Data;

import java.util.Map;

@Data
@Builder
public final class GetStateAction<T> implements DaprAction<GetStateResult<T>> {

    private String storeName;
    private String key;
    private StateConsistency consistency;
    private Map<String, String> metadata;
    private DaprSerializer daprSerializer;
    private Class<T> clazz;

    /**
     * @param daprClient
     * @return
     */
    @Override
    public GetStateResult<T> invoke(DaprClient daprClient) throws Exception {
        DaprProtos.GetStateRequest request = DaprProtos.GetStateRequest
                .newBuilder()
                .setStoreName(storeName)
                .setKey(key)
                .setConsistencyValue(consistency.ordinal())
                .putAllMetadata(metadata)
                .build();
        DaprProtos.GetStateResponse response = daprClient.getState(request);
        return new GetStateResult<>(daprSerializer.deserialize(response.getData().toByteArray(), clazz), response.getEtag(), response.getMetadataMap());
    }
}
